<?php
/**
 * FlickrPhotosetsRemove class file
 * @package FlickrPhotosets
 */

/**
 * Removes a user from the database when Facebook notifies that they
 * have removed the app.
 * 
 * @package FlickrPhotosets
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @version 2.0
 * @date 9/14/07
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 * @since Class available since Release 2.0
 */
class FlickrPhotosetsRemove extends FlickrPhotosets {
	
	/**
	 * Verifies that the call has been made from Facebook and launches
	 * the appropriate action.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	public function __construct() {
		parent::__construct();
				
		// Verify the signature and removee
		if ($this->verify_signature()) {
			
			// Log if unsuccessful
			if (!$this->remove_user()) {
				$this->log_failed_removal();
			}
			
		}
		else {
			$this->log_nonfb_attempt();
		}
	}
	
	/**
	 * Logs the failed removal of a user.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	private function log_failed_removal() {
		
		/*
		Will log this information somehow...
		*/
		
		$uid = $_POST['fb_sig_user'];
		
	}
	
	/**
	 * Logs removal attempts that do not come from Facebook.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 */
	private function log_nonfb_attempt() {
		
		/*
		Will log this information somehow...
		*/
		
		$ip = $_SERVER['REMOTE_ADDR'];
		$time = $_SERVER['REQUEST_TIME'];
		$method = $_SERVER['REQUEST_METHOD'];
		$request = $_REQUEST;
				
	}
	
	/**
	 * Removes the user from the database. Gets the UID from fb_sig_user.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @return boolean True if the query was successful
	 */
	private function remove_user() {
		$query = sprintf("DELETE FROM %s WHERE facebook_uid = '%d' LIMIT 1", DB_TABLE, $this->sql_clean($_POST['fb_sig_user']));
		mysql_query($query);
		
		return (mysql_affected_rows() > 0) ? true : false;
	}
	
	/**
	 * Verifies the post variables to ensure the request is coming from Facebook.
	 * 
	 * @author Brad Dougherty <bdougherty@gmail.com>
	 * @return bool True if verified
	 */
	private function verify_signature() {
		return ($_POST['fb_sig'] == Facebook::generate_sig($this->facebook->fb_params, FACEBOOK_API_SECRET)) ? true : false;
	}
	
}

?>